/**
 *
 * Created by spatra on 14-5-7.
 */

$(document).ready( function(){
    $('#byEnterDiv').show();

    var inputMethodId = 1;
    var swapMethodId = 1;
    var proPhyPage = 3;
    var inputString = null;
    var fileLoaded = false;

    $('#byFile').change( function(){
        var reader = new FileReader();
        var file = $('#byFile')[0].files[0];

        reader.onload = function(e){
            inputString = e.target.result;
            fileLoaded = true;
            $('#startBtn').attr('disabled', false);
        };

        reader.readAsText(file);
    });

    $('#inputMethod').change( function(event){
        var inputMethod = $(this).find('option:selected').val();

        if( inputMethod == 1 ){
            $('#byFileDiv').slideUp();
            $('#byEnterDiv').slideDown();
            inputMethodId = 1;
            fileLoaded = false;
        }else{
            $('#byEnterDiv').slideUp();
            $('#byFileDiv').slideDown();
            inputMethodId = 2;

            $('#startBtn').attr('disabled', true);
        }

    });

    $('#swapMethod').change( function(event){
        swapMethodId = $(this).find('option:selected').val() * 1;
    });

    $('#proPhyPage').change( function(event){
        proPhyPage = $(this).val() * 1;
        if( proPhyPage < 0 )
            alert('请输入正整数！');
    });

    $('#startBtn').click( function(event){
        event.preventDefault();
        $('#simulation').find('.methodDiv').remove();
        $(this).attr('disabled', true);

        if( inputMethodId == 1 ){
            inputString = $('#byEnter').val();
            inputString = $.trim(inputString);
            if( inputString == '' ) {
                alert("请输入正确的调度队列！");
                return false;
            }

        }else{
            if( fileLoaded != true ) {
                alert('请选择正确格式的存有调度队列的文件！')
                return false;
            }
        }

        var theQueue =  getSchedulingQueue(inputString);
        var methods = new Array();

        switch( swapMethodId ){
         case 1:
         case 2:
         case 3:
            methods.push( eval( 'new swap'+ $('#swapMethod option:selected').text() +'(' + proPhyPage +')' ) );
            break;
         case 4:
             methods.push( new swapFIFO(proPhyPage) );
             methods.push( new swapLRU(proPhyPage) );
             methods.push( new swapOPT(proPhyPage, theQueue) );
             break;
         default:
             break;
        }

        resetSimulation(theQueue, methods);
        simulation();
    });

});


function readFromFile(){
    var file = $('#byFile')[0].file[0];
    var reader = new FileReader();


    reader.onload = function(e){

    }
}
